package com.StarStudio.Service.Impl;

import com.StarStudio.DAO.CouponsDAO;
import com.StarStudio.Service.CouponsStockService;
import com.StarStudio.Service.PanicBuyingCouponsService;
import com.ruoyi.common.core.Entity.*;
import com.StarStudio.Service.CouponsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class CouponsServiceImpl implements CouponsService {

    @Autowired
    CouponsDAO couponsDAO;

    @Autowired
    CouponsStockService couponsStockService;

    @Autowired
    PanicBuyingCouponsService panicBuyingCouponsService;

    @Override
    public List<Coupons> findCouponsList(Coupons coupons) {
        return couponsDAO.findCouponsList(coupons);
    }

    @Override
    public void addCoupons(Coupons coupons) {
        //向优惠券库存明细中添加数据
        int Stock = coupons.getStock();
        String url = "http://yibinmall.chenglee.top:8080/exchange/check/";
        couponsDAO.addCoupons(coupons);
        if (Stock > 0){
            for (int i = 0; i < Stock; i++) {
                CouponsStock couponsStock = new CouponsStock();
                couponsStock.setCoupons_id(coupons.getCoupon_id());
                couponsStock.setHave_been_sold(false);
                couponsStock.setAdd_time(new Timestamp(new Date().getTime()));
                couponsStock.setCoupons_name(coupons.getCoupon_name());
                couponsStockService.addCouponsStock(couponsStock);
                couponsStock.setQr_code(url + couponsStock.getItem_id());
                couponsStockService.updateCouponsStock(couponsStock);
            }
        }
    }

    @Override
    public void DeleteCouponsById(long id) {
        //删除优惠券的同时，真正删除未售出的库存个体
        List<Long> unsold_coupons_item_ids = couponsStockService.findUnsoldItemIdsByCouponsId(id);
        for (Long unsold_coupons_item_id : unsold_coupons_item_ids) {
            couponsStockService.realDelete(unsold_coupons_item_id);
        }
        //逻辑删除已售出的库存个体
        List<Long> sold_coupons_item_ids = couponsStockService.findSoldItemIdsByCouponsId(id);
        for (Long sold_coupons_item_id : sold_coupons_item_ids) {
            couponsStockService.updateDelFlag(sold_coupons_item_id);
        }
        //逻辑删除对应的抢购优惠券
        PanicBuyingCoupons byCouponsId = panicBuyingCouponsService.findByCoupons_id(id);
        if (byCouponsId != null){
            byCouponsId.setDel_flag(true);
            panicBuyingCouponsService.updatePanicBuyingCoupons(byCouponsId);
        }
        //删除优惠券时，是逻辑删除，其实还保存在数据库中
        couponsDAO.deleteCouponsById(id);
    }

   public void updateCoupons(Coupons coupons){
       int couponsNewStock = coupons.getStock();
       int couponsOldStock = couponsDAO.findStockByCouponsId(coupons.getCoupon_id());
       if (couponsNewStock > couponsOldStock){  /*如果增加库存*/
           //向优惠券库存个体表添加数据
           CouponsStock couponsStock = new CouponsStock();
           couponsStock.setCoupons_id(coupons.getCoupon_id());
           couponsStock.setHave_been_sold(false);
           couponsStock.setAdd_time(new Timestamp(new Date().getTime()));
           couponsStock.setCoupons_name(coupons.getCoupon_name());
           int addNum = couponsNewStock - couponsOldStock;
           for (int i = 0; i < addNum; i++) {
               couponsStockService.addCouponsStock(couponsStock);
           }
       }else if (couponsNewStock < couponsOldStock){ /*如果减少库存*/
           //删除多余优惠券库存
           int subNum = couponsOldStock - couponsNewStock;
           List<Long> couponsStockIds = couponsStockService.findCouponsStockIds(coupons.getCoupon_id());
           for (int i = 0; i < subNum; i++) {
               couponsStockService.realDelete(couponsStockIds.get(i));
           }
       }
       couponsDAO.updateCoupons(coupons);
   }

    /**
     * 查询总记录数
     * @return 总记录数
     */
    @Override
    public long findAllRecordNumber() {
        return couponsDAO.findAllRecordNumber();
    }

    @Override
    public Coupons findByCoupons_item_id(long coupon_item_id) {
        return couponsDAO.findByCoupons_item_id(coupon_item_id);
    }

    @Override
    public Coupons findByCouponsId(long coupons_id) {
        return couponsDAO.findByCoupon_id(coupons_id);
    }

    @Override
    public List<CouponsChooseResult> getCouponsChooseResultList() {
        List<CouponsChooseResult> couponsChooseResultList = couponsDAO.getCouponsChooseResultList();
        for (CouponsChooseResult couponsChooseResult : couponsChooseResultList) {
            int Stock1 = couponsChooseResult.getStock();
            int Stock2 = 0;
            PanicBuyingCoupons byCoupons_id = panicBuyingCouponsService.findByCoupons_id(couponsChooseResult.getCoupon_id());
            if (byCoupons_id != null){
                couponsChooseResult.setStock(Stock1 + Stock2);
            }else {
                couponsChooseResult.setStock(Stock1);
            }
        }
        return couponsChooseResultList;
    }

    @Override
    public List<String> findLikeStoreName(String name) {
        return couponsDAO.findLikeStoreName(name);
    }

    @Override
    public Coupons findByCoupon_id(long id) {
        return couponsDAO.findByCoupon_id(id);
    }

    @Override
    public int findStockByCouponsId(long couponsId) {
        return couponsDAO.findStockByCouponsId(couponsId);
    }

    @Override
    public void updateStockById(long couponsId, int stock) {
        couponsDAO.updateStockById(couponsId,stock);
    }
}
